import Vue from 'vue';
import VueRouter from 'vue-router';
import store from '@/store';
// 导入网页加载进度条
import Nprogress from 'nprogress';
import 'nprogress/nprogress.css';
import i18n from '@/language';

Vue.use(VueRouter);

const router = new VueRouter({
  linkActiveClass: 'active',
  routes: [
    { path: '/', redirect: '/login' },
    {
      path: '/login',
      component: () => import('@/view/Login.vue'),
      meta: { title: '登录' },
    },

    {
      path: '/index',
      component: () => import('@/layout/index.vue'),
      redirect: '/index/home',
      children: [
        {
          path: 'home',
          name: 'home',
          component: () => import('@/view/Home.vue'),
          // meta: { title: '首页' },
        },
        {
          path: 'books',
          name: 'books',
          component: () => import('@/view/Books.vue'),
          meta: { title: 'booksManagement' },
        },
        {
          path: 'bookstype',
          name: 'bookstype',
          component: () => import('@/view/BooksType.vue'),
          meta: { title: 'typeManagement' },
        },
        {
          path: 'attachment',
          name: 'attachment',
          component: () => import('@/view/Attachment.vue'),
          meta: { title: 'attachment' },
          redirect: 'attachment/picture',
          children: [
            {
              path: 'picture',
              name: 'picture',
              component: () => import('@/view/Picture.vue'),
              meta: { title: 'picture' },
            },
            {
              path: 'video',
              name: 'video',
              component: () => import('@/view/Video.vue'),
              meta: { title: 'video' },
            },
            {
              path: 'audio',
              name: 'audio',
              component: () => import('@/view/Audio.vue'),
              meta: { title: 'audio' },
            },
          ],
        },
        {
          path: 'checkpwd',
          name: 'checkpwd',
          component: () => import('@/view/CheckPwd.vue'),
          meta: { title: '修改密码' },
        },
        {
          path: 'test',
          name: 'test',
          component: () => import('@/view/test.vue'),
          meta: { title: '测试' },
        },
      ],
    },
    {
      path: '*',
      component: () => import('@/view/NotFound.vue'),
    },
  ],
});
// 关闭进度条小圈
Nprogress.configure({ showSpinner: false });
router.beforeEach((to, from, next) => {
  // console.log(i18n.t('menus.home'));
  document.title = to.meta.title
    ? i18n.t(`menus.${to.meta.title}`)
    : i18n.t('menus.home');
  // document.title = to.meta.title;
  Nprogress.start(); // 开启进度条
  let token = store.state.token;
  if (to.path === '/login') {
    if (token) next('/index');
    else next();
  } else {
    if (token) {
      next();
    } else {
      // 没有Token打回登录页面并将想访问的路由作为login路由的字符串参数
      next({ path: '/login', query: { redirect: to.path } });
      // console.log(to.path);
    }
  }
});
// 全局（针对所有路由）后守卫
router.afterEach((to, from) => {
  Nprogress.done(); // 关闭进度条
});
// 先保存原始的 VueRoute push方法的引用
const originPush = VueRouter.prototype.push;
const originReplace = VueRouter.prototype.replace;

// 后重写
VueRouter.prototype.push = function (location, okCallback, failCallback) {
  if (okCallback && failCallback) {
    return originPush.call(this, location, okCallback, failCallback);
  } else {
    return originPush.call(this, location).catch(() => {});
  }
};

VueRouter.prototype.replace = function (location, okCallback, failCallback) {
  if (okCallback && failCallback) {
    return originReplace.call(this, location, okCallback, failCallback);
  } else {
    return originReplace.call(this, location).catch(() => {});
  }
};
export default router;
